<template>
	<div ref="AppVersion">
		<el-dialog title="版本维护" :visible.sync="dialogFormVisible" :close-on-press-escape="false"
			:close-on-click-modal="false" @closed="afterCloseDialog">
			<el-form :model="appVersionForm" ref="appVersionForm" :rules="formRules" label-width="100px">
				<el-row :gutter="100">
					<el-col :md="12">
						<el-form-item label="版本号" prop="code">
							<el-input v-model="appVersionForm.code" maxlength="6"></el-input>
						</el-form-item>
					</el-col>
					<el-col :md="12">
						<el-form-item label="版本名称" prop="name">
							<el-input v-model="appVersionForm.name" maxlength="50"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="100">
					<el-col :md="12">
						<el-form-item label="类型" prop="appType">
							<el-radio-group v-model="appVersionForm.appType">
								<el-radio v-for="item in appTypeList" :label="item.id" :key="item.id">{{item.name}}
								</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :md="12">
						<el-form-item label="强制更新" prop="forceUpdate">
							<el-switch v-model="appVersionForm.forceUpdate" active-value="Y" inactive-value="N"
								active-color="#13ce66" inactive-color="#ff4949" active-text="是" inactive-text="否">
							</el-switch>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :md="24">
						<el-form-item label="下载地址" prop="downloadUrl">
							<el-input v-model="appVersionForm.downloadUrl" maxlength="200"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :md="24">
						<el-form-item label="更新说明" prop="updateDesc">
							<el-input type="textarea" v-model="appVersionForm.updateDesc" rows="5" maxlength="500">
							</el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="100">
					<el-col :md="12">
						<el-form-item label="发布时间" prop="releaseTime">
							<el-date-picker type="datetime" placeholder="选择时间" v-model="appVersionForm.releaseTime"
								format="yyyy-MM-dd HH:mm"></el-date-picker>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible = false">返回</el-button>
				<el-button type="primary" @click="saveForm">保存</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		name: "AppVersion",
		data() {
			return {
				dialogFormVisible: false,
				appVersionForm: {
					code: null,
					name: null,
					forceUpdate: 'N',
					downloadUrl: null,
					appType: 'android',
					updateDesc: null,
					releaseTime: null
				},
				appTypeList: [
					{
						id: "android",
						name: "Android"
					},
					{
						id: "ios",
						name: "IOS"
					}
				],
				formRules: {
					code: [{
						required: true,
						message: '请输入版本号'
					}],
					name: [{
						required: true,
						message: '请输入版本名称'
					}],
					appType: [{
						required: true,
						message: '请选择类型'
					}],
					forceUpdate: [{
						required: true,
						message: '请选择更新方式'
					}],
					downloadUrl: [{
						required: true,
						message: '请输入下载地址'
					}],
					updateDesc:  [{
						required: true,
						message: '请输入更新说明'
					}],
					releaseTime: [{
						required: true,
						message: '请输入发布时间'
					}]
				}
			};
		},
		methods: {
			initForm(id) {
				if (id) {
					this.appVersionForm.id = id;
					this.$api.post('/appVersion/getById', {
						id: id
					}).then(response => {
						this.dialogFormVisible = true;
						let appVersion = response.data;
						this.appVersionForm = appVersion;
					});
				} else {
					this.dialogFormVisible = true;
				}
			},
			saveForm() {
				let url = '/appVersion/add';
				if (this.appVersionForm.id) {
					url = '/appVersion/update';
				}

				this.$refs.appVersionForm.validate(valid => {
					if (valid) {
						this.$api.post(url, this.appVersionForm).then(response => {
							if (response.code === "900") {
								this.$message.success(response.message);
								this.dialogFormVisible = false;
								this.$parent.handleSearch();
							} else {
								this.$message.error(response.message);
							}
						});
					}
				});
			},
			afterCloseDialog() {
				this.appVersionForm = {
					code: null,
					name: null,
					forceUpdate: null,
					downloadUrl: null,
					appType: null,
					updateDesc: null,
					releaseTime: null
				};
				this.$refs.appVersionForm.resetFields();
			}
		}
	};
</script>
